package ${package}.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jieyi.dao.${moduleName}.${ClassName}Mapper;
import com.jieyi.form.ConsoleUser;
import com.jieyi.form.${ClassName};
import com.jieyi.requestData.${ClassName}AddData;
import com.jieyi.requestData.${ClassName}DeleteData;
import com.jieyi.requestData.${ClassName}PagingListData;
import com.jieyi.requestData.${ClassName}UpdateData;
import com.jieyi.util.ConverterForObjectAndMap;
import com.jieyi.util.${ModuleName}PltResult;
import com.jieyi.util.PageUtil;
import com.jieyi.util.SnowflakeIdWorker;
import com.jieyi.util.SortConsts;
import com.jieyi.util.VerifyException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${tableName} ${tableComment}
 *
 * @author JieYi Generator by ${author} ${email}
 * @date ${date}
 */
@Service
@Slf4j
public class ${ClassName}Service {

    @Autowired
    private ${ClassName}Mapper ${className}Mapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    public Map<String, Object> pagingList(${ClassName}PagingListData ${className}PagingListData, ConsoleUser consoleUser) throws Exception {
        Map<String, Object> queryMap = new HashMap<>();
        <#list queryList as field>
        queryMap.put("${field.attrName}", ${className}PagingListData.get${field.attrName?lower_case?cap_first}());
        </#list>

        PageHelper.startPage(Integer.parseInt(${className}PagingListData.getCurrpage()), Integer.parseInt(${className}PagingListData.getRecnumperpage()));
        String orderBy = SortConsts.getSortStr(${className}PagingListData.getOrderby(), ${ModuleName}SortConsts.${ModuleName?upper_case}_ASC_DESC);
        PageHelper.orderBy(orderBy);
        Page<${ClassName}> ${className}Page = ${className}Mapper.selectByPage(queryMap);
        Map<String, Object> returnData = PageUtil.generalPageInfoExpectPageList(${className}Page);

        List<Map<String, Object>> pagelist = new ArrayList<>();
        if (${className}Page.getResult().size() > 0) {
            for (${ClassName} ${className} :${className}Page.getResult()){
                Map<String, Object> map = new HashMap<>();
                map.putAll(ConverterForObjectAndMap.objectToMap(${className}, false, false));
                pagelist.add(map);
            }
        }
        returnData.put("pagelist", pagelist);
        return returnData;
    }

    public Map<String, Object> add(${ClassName}AddData ${className}AddData, ConsoleUser consoleUser) {
        Map<String, Object> returnData = new HashMap<>();
        ${ClassName} ${className} = new ${ClassName}();
        ${className}.setId(snowflakeIdWorker.nextId());
        <#list formList as field>
        ${className}.set${field.attrName?cap_first}(${className}AddData.get${field.attrName?lower_case?cap_first}());
        </#list>
        ${className}Mapper.insert(${className});
        return returnData;
    }

    public Map<String, Object> update(${ClassName}UpdateData ${className}UpdateData, ConsoleUser consoleUser) {
        Map<String, Object> returnData = new HashMap<>();
        ${ClassName} ${className} = new ${ClassName}();
        ${className}.setId(Long.valueOf(${className}UpdateData.getId()));
        <#list formList as field>
        ${className}.set${field.attrName?cap_first}(${className}UpdateData.get${field.attrName?lower_case?cap_first}());
        </#list>
        ${className}Mapper.update(${className});
        return returnData;
    }

    public Map<String, Object> delete(${ClassName}DeleteData ${className}DeleteData, ConsoleUser consoleUser) {
        Map<String, Object> returnData = new HashMap<>();
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("id", Long.parseLong(${className}DeleteData.getId()));
        ${className}Mapper.delete(deleteMap);
        return returnData;
    }

}